package cn.good.yan.service.t3;

import java.util.concurrent.TimeUnit;

/**
 * 3. 8锁的范围
 * 问题二 : 在发短信方法中，延迟4秒，两个线程先打印 发短信 还是 打电话？
 *
 * @author shengren.yan
 * @create 2024-03-26
 */
public class Test2 {
    public static void main(String[] args) throws Exception {
        Phone2 phone = new Phone2();
        // 线程A
        new Thread(() -> {
            phone.seedMsg();
        }, "A").start();
        // 1秒延迟
        TimeUnit.SECONDS.sleep(1);
        // 线程B
        new Thread(() -> {
            phone.call();
        }, "B").start();
    }
}

class Phone2 {
    public synchronized void seedMsg() {
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("发短信");
    }

    public synchronized void call() {
        System.out.println("打电话");
    }
}

// 还是先打印发短信。